package com.fyw.xmcamera;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.fyw.xmcamera.adapter.LanListAdapter;
import com.lib.MsgContent;
import com.lib.funsdk.support.FunSupport;
import com.lib.funsdk.support.OnAddSubDeviceResultListener;
import com.lib.funsdk.support.OnFunDeviceListener;
import com.lib.funsdk.support.OnFunLoginListener;
import com.lib.funsdk.support.models.FunDevice;
import com.lib.funsdk.support.models.FunLoginType;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by FYW on 2017/10/19.
 */

public class DeviceListActivity extends BaseActivity implements OnFunDeviceListener, OnFunLoginListener, OnAddSubDeviceResultListener {
    private List<FunDevice> mLanDeviceList = new ArrayList<>();
    private ListView deviceListLanListView;
    private LanListAdapter mAdapter;

    private final int MESSAGE_REFRESH_DEVICE_STATUS = 0x100;

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_REFRESH_DEVICE_STATUS: {
                    FunSupport.getInstance().requestAllLanDeviceStatus();
                    mAdapter.notifyDataSetChanged();
                }
                break;
            }
        }

    };

    @Override
    protected void handleToolbar(ToolbarHelper toolbarHelper) {
        toolbarHelper.setTitle("设备列表");
        toolbarHelper.setFinish().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        toolbarHelper.setAdd().setVisibility(View.VISIBLE);
        toolbarHelper.setAdd().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(AddDeviceActivity.class);
            }
        });
    }

    @Override
    public void initParam(Bundle param) {

    }

    @Override
    public int bindLayout() {
        return R.layout.activity_device_list_lan;
    }

    @Override
    public void initView(View view) {
        deviceListLanListView = view.findViewById(R.id.deviceListLanListView);
        mAdapter = new LanListAdapter(DeviceListActivity.this, mLanDeviceList);
        deviceListLanListView.setAdapter(mAdapter);

        // 切换到网络访问访问方式
        FunSupport.getInstance().setLoginType(FunLoginType.LOGIN_BY_INTENTT);

        // 监听设备类事件
        FunSupport.getInstance().registerOnFunDeviceListener(this);

        // 注册监听(用户登录相关)
        FunSupport.getInstance().registerOnFunLoginListener(this);

        FunSupport.getInstance().registerOnAddSubDeviceResultListener(this);

        FunSupport.getInstance().login("lc502", "0000000000");
    }

    @Override
    public void doBusiness(Context mContext) {
        deviceListLanListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bundle bundle = new Bundle();
                bundle.putInt("FUN_DEVICE_ID", mLanDeviceList.get(i).getId());
                startActivity(PlayActivity.class, bundle);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.device_list_menu, menu);
        return true;
    }

    private void refreshLanDeviceList() {
        mLanDeviceList.clear();

        mLanDeviceList.addAll(FunSupport.getInstance().getDeviceList());

        // 延时100毫秒更新设备消息
        mHandler.removeMessages(MESSAGE_REFRESH_DEVICE_STATUS);
        if (mLanDeviceList.size() > 0) {
            mHandler.sendEmptyMessageDelayed(MESSAGE_REFRESH_DEVICE_STATUS, 100);
        }
    }

    @Override
    public void onDeviceListChanged() {
        refreshLanDeviceList();
    }

    @Override
    public void onDeviceStatusChanged(FunDevice funDevice) {
        if (null != mAdapter) {
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDeviceAddedSuccess() {

    }

    @Override
    public void onDeviceAddedFailed(Integer errCode) {

    }

    @Override
    public void onDeviceRemovedSuccess() {

    }

    @Override
    public void onDeviceRemovedFailed(Integer errCode) {

    }

    @Override
    public void onAPDeviceListChanged() {

    }

    @Override
    public void onLanDeviceListChanged() {

    }

    //----------------------------------------------登录------------------------------------------------
    @Override
    public void onLoginSuccess() {
        // 刷新设备列表
        refreshLanDeviceList();
    }

    @Override
    public void onLoginFailed(Integer errCode) {
        showToast(errCode + "");
    }

    @Override
    public void onLogout() {

    }

    @Override
    public void onAddSubDeviceFailed(FunDevice funDevice, MsgContent msgContent) {

    }

    @Override
    public void onAddSubDeviceSuccess(FunDevice funDevice, MsgContent msgContent) {

    }
}
